<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
			<el-form-item label="材料名称" prop="materialName">
				<el-input v-model="queryParams.materialName" placeholder="请输入材料名称" clearable size="small" @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="取票编码" prop="ticketCollectionCode">
				<el-input v-model="queryParams.ticketCollectionCode" placeholder="请输入取票编码" clearable size="small" @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item>
				<el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:material:add']">新增</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="materialList" row-key="id" default-expand-all :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
			<el-table-column prop="materialName" label="材料名称" width="260"></el-table-column>
			<el-table-column prop="orderNum" label="排序" width="200"></el-table-column>
			<el-table-column prop="ticketCollectionCode" label="取票编码" width="100"></el-table-column>
			<el-table-column prop="materialSpec" label="材料规格" width="100"></el-table-column>
			<el-table-column prop="materialUnit" label="材料单位" width="100"></el-table-column>
			<el-table-column prop="totalStock" label="总库存" width="100"></el-table-column>
			<el-table-column prop="surplusStock" label="剩余库存" width="100"></el-table-column>
			<el-table-column prop="supplier" label="供应商" width="100"></el-table-column>
			<el-table-column label="创建时间" align="center" prop="createTime" width="200">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.createTime) }}</span>
				</template>
			</el-table-column>
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:material:edit']">修改</el-button>
					<el-button size="mini" type="text" icon="el-icon-plus" @click="handleAdd(scope.row)" v-hasPermi="['system:material:add']">新增</el-button>
					<el-button v-if="scope.row.parentId != 0" size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['system:material:remove']">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!-- 添加或修改材料对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-row>
					<el-col :span="24" v-if="form.parentId !== 0">
						<el-form-item label="上级材料" prop="parentId">
							<treeselect v-model="form.parentId" :options="materialOptions" :normalizer="normalizer" placeholder="选择上级材料" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="材料编号" prop="materialNum">
							<el-input v-model="form.materialNum" placeholder="请输入材料编号" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="材料名称" prop="materialName">
							<el-input v-model="form.materialName" placeholder="请输入材料名称" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="显示排序" prop="orderNum">
							<el-input-number v-model="form.orderNum" controls-position="right" :min="0" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="取票编码" prop="ticketCollectionCode">
							<el-input v-model="form.ticketCollectionCode" placeholder="请输入取票编码" maxlength="20" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="材料规格" prop="materialSpec">
							<el-input v-model="form.materialSpec" placeholder="请输入材料规格" maxlength="11" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="材料单位" prop="materialUnit">
							<el-input v-model="form.materialUnit" placeholder="请输入材料单位" maxlength="50" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="总库存" prop="totalStock">
							<el-input-number style="width:200px" size="small" slot-scope="scope" v-model="form.totalStock" :step="0.01" :max="9999999" placeholder="请输入总库存"/>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="剩余库存" prop="surplusStock">
							<el-input-number style="width:200px" size="small" slot-scope="scope" v-model="form.surplusStock" :step="0.01" :max="9999999" placeholder="请输入剩余库存"/>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="警示库存" prop="warningStock">
							<el-input-number style="width:200px" size="small" slot-scope="scope" v-model="form.warningStock" :step="0.01" :max="9999999" placeholder="请输入警示库存"/>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="供应商" prop="supplier">
							<el-input v-model="form.supplier" placeholder="请输入供应商" maxlength="50" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="备注" prop="remark">
							<el-input v-model="form.remark" placeholder="请输入备注" maxlength="50" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import { listMaterialLibrary, getMaterialLibrary, addMaterialLibrary, updateMaterialLibrary, delMaterialLibrary, listMaterialLibraryExcludeChild } from "@/api/business/materialLibrary";
	import Treeselect from "@riophae/vue-treeselect";
	import "@riophae/vue-treeselect/dist/vue-treeselect.css";

	export default {
		name: "MaterialLibrary",
		components: {
			Treeselect
		},
		data() {
			return {
				// 遮罩层
				loading: true,
				// 显示搜索条件
				showSearch: true,
				// 表格树数据
				materialList: [],
				// 材料树选项
				materialOptions: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 1000,
					materialName: undefined,
					ticketCollectionCode: undefined
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {
					parentId: [{
						required: true,
						message: "上级材料不能为空",
						trigger: "blur"
					}],
					materialName: [{
						required: true,
						message: "材料名称不能为空",
						trigger: "blur"
					}],
					orderNum: [{
						required: true,
						message: "菜单顺序不能为空",
						trigger: "blur"
					}],
					email: [{
						type: "email",
						message: "'请输入正确的邮箱地址",
						trigger: ["blur", "change"]
					}],
					phone: [{
						pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
						message: "请输入正确的手机号码",
						trigger: "blur"
					}]
				}
			};
		},
		created() {
			this.getList();
		},
		methods: {
			/** 查询材料列表 */
			getList() {
				this.loading = true;
				listMaterialLibrary(this.queryParams).then(response => {
					this.materialList = this.handleTree(response.rows, "id");
					this.loading = false;
				});
			},
			/** 转换材料数据结构 */
			normalizer(node) {
				if(node.children && !node.children.length) {
					delete node.children;
				}
				return {
					id: node.id,
					label: node.materialName,
					children: node.children
				};
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					id: undefined,
					parentId: undefined,
					materialName: undefined,
					orderNum: undefined,
					leader: undefined,
					phone: undefined,
					email: undefined,
					status: "0"
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			/** 新增按钮操作 */
			handleAdd(row) {
				this.reset();
				if(row != undefined) {
					this.form.parentId = row.id;
				}
				this.open = true;
				this.title = "添加材料";
				listMaterialLibrary({
					pageNum: 1,
					pageSize: 1000,
				}).then(response => {
					this.materialOptions = this.handleTree(response.rows, "id");
				});
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				getMaterialLibrary(row.id).then(response => {
					this.form = response.data;
					this.open = true;
					this.title = "修改材料";
				});
				listMaterialLibraryExcludeChild(row.id).then(response => {
					this.materialOptions = this.handleTree(response.data, "id");
				});
			},
			/** 提交按钮 */
			submitForm: function() {
				this.$refs["form"].validate(valid => {
					if(valid) {
						if(this.form.id != undefined) {
							updateMaterialLibrary(this.form).then(response => {
								if(response.code === 200) {
									this.msgSuccess("修改成功");
									this.open = false;
									this.getList();
								}
							});
						} else {
							addMaterialLibrary(this.form).then(response => {
								if(response.code === 200) {
									this.msgSuccess("新增成功");
									this.open = false;
									this.getList();
								}
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				this.$confirm('是否确认删除名称为"' + row.materialName + '"的数据项?', "警告", {
					confirmButtonText: "确定",
					cancelButtonText: "取消",
					type: "warning"
				}).then(function() {
					return delMaterialLibrary(row.id);
				}).then(() => {
					this.getList();
					this.msgSuccess("删除成功");
				}).catch(function() {});
			}
		}
	};
</script>